package com.dudu.service.support;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.dudu.bean.IfControlBean;
import com.dudu.bean.IfControlRuleBean;
import com.dudu.dao.InfoControlDao;
import com.dudu.dao.InfoControlRuleDao;

/**
 * 信控抽象服务类
 * 
 * @author zx
 *
 */
public abstract class AbstractInfoControlProcessor implements IInfoControlProcessor {

	protected static final Logger log = Logger.getLogger(AbstractInfoControlProcessor.class.getSimpleName());
    
	/*
	 * 请求基本数据
	 */
	protected IfControlBean ifcBean = null;
    
	/*
	 * 其他参数
	 */
	protected Map parameterMap = null;

	protected static InfoControlDao ifDao = new InfoControlDao();

	protected static InfoControlRuleDao ifrDao = new InfoControlRuleDao();
    
	/*
	 * 待匹配的信控规则
	 */
	private List<IfControlRuleBean> ifcRules = null;

	/**
	 * 构造函数
	 * 
	 * @param ifcBean
	 * @param ifcRules
	 * @param parameterMap
	 */
	public AbstractInfoControlProcessor(IfControlBean ifcBean, List<IfControlRuleBean> ifcRules,
			Map<String, String> parameterMap) {
		this.ifcBean = ifcBean;
		if (parameterMap != null)
			this.parameterMap = parameterMap;
		if (ifcRules != null)
			this.ifcRules = ifcRules;
	}
    
	/**
	 * 信控处理的入口
	 */
	@Override
	public void process() {
		if (ifcRules == null || ifcRules.isEmpty())
			return;
		IfControlBean ifBean = ifDao.queryIfControlByAccountInfo(ifcBean);
		IfControlRuleBean ruleBean = null;
		if (ifBean != null) {
			ruleBean = ifBean.getRule();
			/**
			 * 在信控表中存在记录 不需要发送短信和试听通知，只需要更新t_send_notice表的发送状态和发送时间
			 */
			ifControlProcess(ifcBean, ruleBean, true);
		} else {
			InfoControlRuleProcessor ruleProcessor = new InfoControlRuleProcessor(ifcRules);
			ruleBean = ruleProcessor.match(this);
			if (ruleBean != null) {
				ifControlProcess(ifcBean, ruleBean, false);
			}else{
				log.info(ifcBean+"没有匹配中信控监控规则，不需要进行信控监控");
			}
		}
	}

	/**
	 * 目前已经请求的数量 子类实现
	 * 
	 * @return
	 */
	protected abstract int alreadyRequestCount(IfControlBean ifcBean);

	/**
	 * 进入信控流程 子类实现
	 * 
	 * @param requestBean
	 *            请求数据
	 * @param rule
	 *            匹配中的规则
	 * @param startedIfControl
	 *            当前批次号下是否已启动信控
	 */
	protected abstract void ifControlProcess(IfControlBean ifcBean, IfControlRuleBean rule, boolean startedIfControl);

}
